package com.zbest.zpay.web.controller;

import javax.annotation.Resource;

import net.spy.memcached.transcoders.SerializingTranscoder;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zbest.component.session.RedisSession;
import com.zbest.component.session.SessionRepository;
import com.zbest.zpay.integration.redis.RedisClient;

@Controller
public class TestController extends BaseController {

    @Resource
    private RedisClient       redisClient;

    @Resource
    private SessionRepository sessionRepository;

    @ResponseBody
    @RequestMapping(value = "/shareProfitTask", method = RequestMethod.GET)
    public String shareProfitTask() {
        return newSuccessResult();
    }

    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {

        return "{\"result\":\"ok\"}";
    }

    @ResponseBody
    @RequestMapping(value = "/testocs", method = RequestMethod.GET)
    public String testocs() {
        SerializingTranscoder t = new SerializingTranscoder();
        redisClient.putData("hello", "world");

        RedisSession session = new RedisSession();
        session.setSessionId("abcd");

        redisClient.putData("abcd", t.encode(session).getData(), 1000);
        redisClient.putData("abcde", "abcde", 1000);
        redisClient.putData("abcde", new byte[] { 1, 2 }, 1000);

        session.setSessionId("aaa");
        sessionRepository.save(session);
        //return ToStringBuilder.reflectionToString(sessionRepository.getSession("abcd"),ToStringStyle.SHORT_PREFIX_STYLE);
        //return newSuccessResult(sessionRepository.getSession("abcd"));
        return ToStringBuilder.reflectionToString(redisClient.getData("abcd"),
            ToStringStyle.SHORT_PREFIX_STYLE);
    }

    @ResponseBody
    @RequestMapping(value = "/test/refreshComponentToken", method = RequestMethod.GET)
    public String refreshComponentToken() {

        return "refreshComponentToken";
    }

    @ResponseBody
    @RequestMapping(value = "/test/refreshAuthorizerToken", method = RequestMethod.GET)
    public String refreshAuthorizerToken() {

        return "refreshAuthorizerToken";
    }

    @RequestMapping(value = "/test/appauth.html", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/test/authcallback.html", method = RequestMethod.GET)
    public String authcallback() {
        return "index2";
    }

}
